//
//  EPMHomeViewController.m
//  WeiBoByOC
//
//  Created by 马继鵬 on 17/3/19.
//  Copyright © 2017年 7TH. All rights reserved.
//

#import "EPMHomeViewController.h"
#import "EPMOtherViewController.h"
#import "EPMHomeTableViewCell.h"
#import "EPMHomeViewModel.h"
#import "EPMRefreshControl.h"

@interface EPMHomeViewController ()
@property (strong,nonatomic) UIActivityIndicatorView* footView;
@property (strong,nonatomic) EPMRefreshControl* refreshView;
@property (weak,nonatomic) UILabel* loadTitle;
@end
static NSString* cell_ID = @"cell_ID";
@implementation EPMHomeViewController
    

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addPushBtn];
    if (!self.isLogin){
        
        [ self upDateTitle: @"关注一些人,回到这里看看有什么惊喜哟" imageName: @"visitordiscover_feed_image_smallicon"isHome: true];
        return;
    }
    
    [self setupUI];
    [self loadData];
}



#pragma
#pragma-UI设置
- (void)setupUI{
    
    [self.tableView registerClass:[EPMHomeTableViewCell class] forCellReuseIdentifier:cell_ID];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 300;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = self.footView;
    
    [self.tableView addSubview:self.refreshView];
    [self addLoadLabel];
    
}

- (void)addLoadLabel{
    UILabel* loadLabel = [UILabel EPM_Title:@"" textColor:[UIColor whiteColor] fontSize:FONTSIZENORMAL];
    [self.navigationController.view insertSubview:loadLabel belowSubview:self.navigationController.navigationBar];
    self.loadTitle = loadLabel;
    loadLabel.frame = CGRectMake(0,34, screenWidth, 30);
//    loadLabel.hidden = true;
    loadLabel.backgroundColor = THEMECOLOR;
    loadLabel.textAlignment = NSTextAlignmentCenter;
    
}

- (void)loadData {
    
    [[EPMHomeViewModel sheardTool] getHomeViewDataWithPullGesture:self.footView.isAnimating finished:^(BOOL success, NSUInteger count) {
        if (!success){
            [SVProgressHUD showErrorWithStatus:@"世界上最遥远的地方是没有网"];
        }else{
            [self.tableView reloadData];
        }
        self.loadTitle.text = count > 0 ? [NSString stringWithFormat:@"更新了%lu条微博",count]: @"已经是最新的内容";
//        self.loadTitle.hidden = false;
        [UIView animateWithDuration:0.25 animations:^{
            self.loadTitle.transform = CGAffineTransformMakeTranslation(0, 30);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.25 delay:2 options:0 animations:^{
                self.loadTitle.transform = CGAffineTransformIdentity;
            } completion:^(BOOL finished) {
//                self.loadTitle.hidden = true;
            }];
        }];
        
        [self.footView stopAnimating];
        [self.refreshView endRefreshing];
    }];
    
    
}



#pragma
#pragma-数据源方法设置
    - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return [EPMHomeViewModel sheardTool].dataArray.count;
    }
    
    
    - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        EPMHomeTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cell_ID forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.statuesModel = [EPMHomeViewModel sheardTool].dataArray[indexPath.item];
        return cell;
        
    }
    
#pragma
#pragma-代理方法

        - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
            if (indexPath.row == [EPMHomeViewModel sheardTool].dataArray.count-1 && !self.footView.isAnimating){
                [_footView startAnimating];
                [self loadData];
            }
           
        }


    
    
    
    
    
    
    
    
    
    
    
    
    
    
#pragma
#pragma-懒加载控件
    
- (UIActivityIndicatorView*)footView{
    
    if ( _footView == nil){
        _footView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        _footView.color = THEMECOLOR;
    }
    return _footView;
}
    
- (EPMRefreshControl*) refreshView{
    if (_refreshView == nil){
        EPMRefreshControl* view = [[EPMRefreshControl alloc]init];
       [ view addTarget:self action:@selector(refreshingData) forControlEvents:UIControlEventValueChanged];
        _refreshView = view;
    }
    return _refreshView;
}
    

    
    
#pragma
#pragma-添加按钮
- (void)addPushBtn{
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem EPM_imageName:@"navigationbar_pop" target:self action:@selector(pushToNew:)];
    
}
#pragma
#pragma-点击事件
- (void)pushToNew:(id)sender
{
    EPMOtherViewController* otherController = [[EPMOtherViewController alloc]init];
    
    [self.navigationController pushViewController:otherController animated:YES];
}
- (void)refreshingData{
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
